package de.clapp.newsfeed;

import java.util.ArrayList;

import android.content.AsyncTaskLoader;
import android.content.Context;
import de.clapp.backend.network.ClientRequest;
import de.clapp.backend.network.TCPSocket;
import de.clapp.backend.requests.NewsFeedRequest;
import de.clapp.data.Newsfeed;
import de.clapp.data.settings.ServerSettings;
import de.clapp.toolkit.Temporary;

public class NewsfeedLoader extends AsyncTaskLoader<ArrayList<Newsfeed>>
{   
    private ArrayList<Newsfeed> newsfeeds;

    private int newsToLoad;

    public NewsfeedLoader(Context context, int newsToLoad)
    {
        super(context);
        this.newsToLoad = newsToLoad;
    }

    @Override
    public ArrayList<Newsfeed> loadInBackground()
    {
        NewsFeedRequest request = new NewsFeedRequest(newsToLoad);
        // Serververbindung
        try
        {
            System.out.println("NewsfeedLoader.loadInBackground() 1");
            ClientRequest r = new ClientRequest(new TCPSocket(de.clapp.Settings.ServerIP, ServerSettings.ServerPort));
            System.out.println("NewsfeedLoader.loadInBackground() 2");
            r.request(request);
            System.out.println("NewsfeedLoader.loadInBackground() 3");
            newsfeeds = request.getItem();
            r.close();
            System.out.println("NewsfeedLoader.loadInBackground() 4");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return newsfeeds;
    }
}